package com.travel.travel_springboot_web.mapper;

import com.travel.travel_springboot_web.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {
    @Insert("INSERT INTO user (openid, nickname, avatar, role, status, created_at, updated_at) " +
            "VALUES (#{openid}, #{nickname}, #{avatar}, #{role}, #{status}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);
    
    @Select("SELECT * FROM user WHERE openid = #{openid}")
    User findByOpenid(String openid);
    
    @Update("UPDATE user SET nickname = #{nickname}, avatar = #{avatar}, " +
            "updated_at = NOW() WHERE id = #{id}")
    int update(User user);
    
    @Select("SELECT * FROM user ORDER BY created_at DESC")
    List<User> findAll();
    
    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(Long id);
    
    @Select("SELECT name FROM address WHERE user_id = #{userId}")
    String findNameById(Long userId);
    
    @Select("SELECT id FROM user WHERE openid = #{openid}")
    Long findUserIdByOpenid(String openid);
    
    @Update("UPDATE user SET name = #{name}, phone = #{phone}, " +
            "updated_at = NOW() WHERE id = #{id}")
    int updateUserInfo(@Param("id") Long id, 
                      @Param("name") String name, 
                      @Param("phone") String phone);
    
    @Select("SELECT name, phone FROM user WHERE id = #{id}")
    Map<String, String> findUserInfoById(Long id);
} 